package se19team1e.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import se19team1e.controller.Application;

/**
 * The message box helper provides helper methods to display common message boxes.
 * 
 * @author lai shiaw san
 *
 */
public class MessageBoxHelper {

    /**
     * A reference to the current application instance.
     */
    private final Application application;
    

    /**
     * Constructs a helper, initializing with the application instance.
     * @param application Reference to the current application instance.
     */
    public MessageBoxHelper( Application application ) {
        this.application = application;
    }
    
    /**
     * Helper method to display a message box.
     * 
     * @param title Title to display.
     * @param message Message to display.
     * @param actionListener Callback when ok button is triggered in message box.
     */
    public void displayMessage( String title, String message, final ActionListener actionListener ) {
        MessageBoxFactory messageBoxFactory = this.application.getMessageBoxFactory();
        
        BaseScreen currentScreen = this.application.getCurrentScreen();
        final MessageBox messageBox = messageBoxFactory.createMessageBox( currentScreen );
        
        messageBox.setTitle( title );
        messageBox.setMessage( message );
        messageBox.setActionListener( new ActionListener() {
            @Override
            public void actionPerformed( ActionEvent event ) {
               messageBox.hide();
               messageBox.dispose();
               
               actionListener.actionPerformed( event );
            }
        });
        
        messageBox.show();
    }

    /**
     * Displays a message box indicating that input is missing.
     * 
     * @param field Name of field missing.
     */
    public void displayMissingInputErrorMessage( String field ) {
        this.displayMessage( "Error", "Missing " + field, new ActionListener() {
            @Override
            public void actionPerformed( ActionEvent event ) {
               // do nothing
            }
        });
    }

}
